from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.sms.v20190711 import sms_client, models
from config import Config


def send_message(phone, random_code, template_id="1171792"):
        try:
            # 中国号码
            phone = "{}{}".format("+86", phone)
            cred = credential.Credential(Config.TENCENT_SECRET_ID, Config.TENCENT_SECRET_KEY)
            client = sms_client.SmsClient(cred, Config.TENCENT_CITY)

            req = models.SendSmsRequest()

            req.SmsSdkAppid = Config.TENCENT_APP_ID
            # 短信签名内容: 使用utf-8编码,必须填写已审核过的签名,签名可在[短信控制台]查看

            req.Sign = Config.TENCENT_SIGN
            # 示例如+8615876460963,其中前面有一个+,86为国家码

            req.PhoneNumberSet = [phone, ]
            # 模板ID,已审核的
            req.TemplateID = template_id
            # 模板参数,若无参数,则设置为空
            req.TemplateParamSet = [random_code]



            resp = client.SendSms(req)


            # 输出json格式的字符串回包
            # print(resp.to_json_string(indent=2))
            if resp.SendStatusSet[0].Code == "Ok":
                return True


        except TencentCloudSDKException as err:
            pass

